*This do-file is to execute the coding for the article entitled "Behavioral and financial coping strategies among energy-insecure households"*
*Authors: Sanya Carley, Michelle Graff, David Konisky, Trevor Memmott*
*Corresponding author: Sanya Carley; scarley@indiana.edu*


*Run this do-file first and then follow with the corresponding analysis file, entitled "CopingStrategies_ANALYSIS_2"*

***************
*Import Data
***************

capture log close
log 	using Coding.log, replace text
clear 	all
macro 	drop _all
set 	linesize 80
set 	scheme economist
ssc		install outreg2

use waves_energy_insecurity.dta, replace

**Destring respondent ID's**
destring caseid_w1, generate(id)

***********************************
*Generate and clean variables
***********************************
gen wave1 = (wave==1)
gen wave2 = (wave==2)
gen wave3 = (wave==3)
gen wave4 = (wave==4)

*Regional variables
gen west = (state==3|state==2|state==5|state==6|state==12|state==13|state==27|state==29|state==32|state==38|state==45|state==48|state==51)
gen south = (state==1|state==4|state==8|state==10|state==11|state==18|state==19|state==21|state==25|state==34|state==32|state==34|state==35|state==38|state==40|state==9|state==43|state==41|state==47|state==49|state==44|state==37)
gen northeast = (state==7|state==20|state==22|state==30|state==40|state==46|state==31|state==33|state==39)
gen midwest = (state==14|state==15|state==16|state==17|state==23|state==24|state==26|state==28|state==35|state==36|state==42|state==50)

*Climate regions
gen northeast_climateregion = (state==7|state==8|state==9|state==20|state==21|state==22|state==30|state==31|state==33|state==39|state==40|state==46)
gen uppermidwest_climateregion = (state==16|state==23|state==24|state==50)
gen ohiovalley_climateregion = (state==14|state==15|state==18|state==26|state==36|state==43|state==49)
gen southeast_climateregion = (state==1|state==10|state==11|state==34|state==41|state==47)
gen rockiesplains_climateregion = (state==27|state==28|state==35|state==42|state==51)
gen south_climateregion = (state==4|state==17|state==19|state==25|state==37|state==44)
gen southwest_climateregion = (state==3|state==6|state==32|state==45)
gen northwest_climateregion = (state==13|state==38|state==48)
gen west_climateregion = (state==5|state==29)
gen alaska_climateregion = (state==2)
gen hawaii_climateregion = (state==12)

*Household characteristics

*Homeowner or renter
gen home_owner = (ownhome==1)
recode home_owner *=. if ownhome==.
gen home_renter = (ownhome==2)
recode home_renter *=. if ownhome==.
drop home_other
gen home_other = (ownhome==3)
recode home_other *=. if ownhome==.

*Marital status
gen married_together = (marstat==1|marstat==6)
recode married_together *=. if marstat==.
gen never_married = (marstat==5)
recode never_married *=. if marstat==.
gen marital_apart = (marstat==2|marstat==3|marstat==4)
recode marital_apart *=. if marstat==.
label var marital_apart "Marital status: separated, divorced, or widowed"
label var married_together "Marital status: married or domestic partnership"

*Race
gen asian = (race==4)
recode asian *=. if race==.
gen mixed_race = (race==6)
recode mixed_race *=. if race==.
gen other_race = (race==5|race==7|race==8)
recode other_race *=. if race==.
gen other_race2 = (other_race==1|asian==1|mixed_race==1)

*Education
gen educ_HSorless = (educ==1|educ==2)
gen educ_college = (educ==3|educ==4|educ==5)
gen educ_grad = (educ==6)
recode educ_HSorless *=. if educ==.
recode educ_college *=. if educ==.
recode educ_grad *=. if educ==.

*Housing conditions
gen bad_housecondition = (poor_insulation==1|drafty==1|mold==1|holes==1|plumbing==1|exposed_sockets==1|nonworking_stove_fridge==1|broken_ac==1)

*Housing type
drop home_apartmentcondo
gen hometype_apartmentcondo = (dwelling_type==2)
recode hometype_apartmentcondo *=. if dwelling_type==.

drop home_trailer
gen hometype_trailer = (dwelling_type==1)
recode hometype_trailer *=. if dwelling_type==.

gen hometype_other = (dwelling_type==5)
recode hometype_other *=. if dwelling_type==.

gen hometype_singlefamily = (dwelling_type==3|dwelling_type==4)
recode hometype_singlefamily *=. if dwelling_type==.

*Code all coping strategy variables including those who filled in options under "other" that match our pre-selected options

*Repondent engaged in risky temperature behavior (used stove, used spaceheater, burned trash, used fireplace, used dryer for warmth) 
gen risky_behavior = (stove==1|spaceheater==1|burntrash==1|fireplace==1|dryer==1)
recode risky_behavior *=1 if id==1135696103 & wave==2
recode risky_behavior *=1 if id==1135070785 & wave==3
recode risky_behavior *=1 if id==1136306315 & wave==3
recode risky_behavior *=1 if id==1135174041 & wave==3
recode risky_behavior *=1 if id==1137602609 & wave==3
recode risky_behavior *=1 if id==1135095107 & wave==3
recode risky_behavior *=1 if id==1134350789 & wave==3
recode risky_behavior *=1 if id==1133582059 & wave==3
recode risky_behavior *=1 if id==1135721269 & wave==4
recode risky_behavior *=1 if id==1137555669 & wave==4
recode risky_behavior *=1 if id==1135174041 & wave==4
recode risky_behavior *=1 if id==1136146747 & wave==4

*Did respondent receive WAP or LIHEAP
gen EI_govtasst = (wap==1|liheap==1)

*Did respondent receive any governnment assistance (SNAP, Medicaid, LIHEAP, TANF, SSI, Unemployment Insurance, WAP, or Veterans Benefits)
gen any_govtasst = (snap==1|medicaid==1|liheap==1|tanf==1|ssi==1|unemp_insurance==1|wap==1|vet_benefits==1)

*Did respondent receive payment assistance to help pay an energy bill (from a government agency, their energy provider, friends of family members, a faith based organization, a nonprofit, a payday lender, or a bankloan)
gen any_payasst = (gov_agency==1|energy_provider==1|friend_family==1|faith_org==1|nonprofit==1|payday_lender==1|bankloan==1) 

*Respondent received WAP or LIHEAP or other energy assistance from a government agency
gen govt_asst_comb = EI_govtasst
recode govt_asst_comb *=1 if gov_agency==1

*Respondent received assistance paying an anergy bill from their network or a financial institution
gen network_payasst = (friend_family==1|faith_org==1|nonprofit==1)
gen bank_payasst = (payday_lender==1|bankloan==1)

gen formal_payasst = (bank_payasst==1|govt_asst_comb==1|energy_provider==1)
gen informal_payasst = network_payasst

*Do respondents have any utility debt?
gen some_debt = (utility_debt>0)
recode some_debt *=. if utility_debt==.

*Do respondents have utility debt over $100
gen debt_over100 = (utility_debt>100)
recode debt_over100 *=. if utility_debt==.

*Respondent received any time of payment assistance
gen cope_payasst = (EI_govtasst==1|any_payasst==1)

*Respondent engaged in any time of bill strategy
gen cope_billstrategy = (utilitybill_pay_irregular==1|some_debt==1)
recode cope_billstrategy *=. if wave==1

*Any coping strategy and number of coping strategy variables
gen any_coping = (risky_behavior==1|forego_expenses==1|cope_billstrategy==1|cope_payasst==1)
gen num_coping = (risky_behavior+forego_expenses+cope_billstrategy+cope_payasst) 
gen num_coping2 = (risky_behavior+forego_expenses+utilitybill_pay_irregular+some_debt+bank_payasst+govt_asst_comb+energy_provider+network_payasst)

*Energy insecurity variables*
*create energy insecurity lags including whether these conditions occured in the year before wave 1
tsset id wave
gen ldisconnect = L.disconnect
gen lhhbill = L.hhbill
recode lhhbill *=1 if hhbill_previousyear==1 & wave==1
recode lhhbill *=0 if hhbill_previousyear==0 & wave==1
recode ldisconnect *=1 if disconnect_previousyear==1 & wave==1
recode ldisconnect *=0 if disconnect_previousyear==0 & wave==1

sort id wave
by id: egen temp_prevEI1 = max(hhbill_previousyear)
by id: egen temp_prevEI3 = max(disconnect_previousyear)

gen EI1previously = (L.hhbill==1|L2.hhbill==1|L3.hhbill==1)
recode EI1previously *=1 if temp_prevEI1==1

gen EI3previously = (L.disconnect==1|L2.disconnect==1|L3.disconnect==1)
recode EI3previously *=1 if temp_prevEI3>=1

sort id
by id: egen temp_total = sum(wave)
gen balanced_panelist = (temp_total==10)

sum temp_prevEI1 temp_prevEI3 temp_total
drop temp_prevEI1 temp_prevEI3 temp_total

*Save the final dataset, from which we run all analyses
save CodedCleaned_4waves.dta, replace

log close
